package com.itheima.health.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.health.common.MessageConst;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.Permission;
import com.itheima.health.service.PermissionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * 权限控制器
 */
@Slf4j
@RestController
@RequestMapping("/permission")
public class PermissionController {

    @Reference
    private PermissionService permissionService;

    /**
     * 分页展示权限
     * @param queryPageBean：
     * @return
     */
    @GetMapping("/findPage")
    public Result findPage(QueryPageBean queryPageBean) {

        log.info("[权限-分页展示]data:{}", queryPageBean);

        //调用service 层
        PageResult pageResult = permissionService.findPage(queryPageBean);

        return new Result(true,"权限查询成功",pageResult);
    }

    /**
     * 添加权限
     *
     * @return
     */
    @PostMapping("/add")
    public Result add(@RequestBody Permission permission) {
        log.info("[权限-添加]data:{}",permission);

        permissionService.add(permission);


        return new Result(true,"添加权限成功");
    }

    /**
     * 根据id查询权限
     */

    @GetMapping("/findById")
//    @PreAuthorize("hasAuthority('CHECKITEM_QUERY')")
    public Result findById(Integer id) {
        log.info("[权限-根据id查询]id:{}", id);
        Permission permission = permissionService.findById(id);
        return new Result(true, MessageConst.ACTION_SUCCESS, permission);
    }

    /**
     * 编辑权限
     */
    @PostMapping("/edit")
//    @PreAuthorize("hasAuthority('CHECKITEM_EDIT')")
    public Result edit(@RequestBody Permission permission) {
        log.info("[权限-编辑]data:{}", permission);

        permissionService.edit(permission);

        return new Result(true,"编辑权限成功");
    }

    /**
     * 根据id删除
     */
    @PostMapping("/delete")
//    @PreAuthorize("hasAuthority('CHECKITEM_DELETE')")
    public Result deleteById(Integer id) {
        log.info("[权限-根据id删除]");
        permissionService.deleteById(id);
        return new Result(true, "删除权限成功");
    }


}
